/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import com.service.ConnectionUtils.ConnectionFactory;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author KIRAN
 */
public class LoginServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    public void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException,IOException
    {
        res.setContentType("text/html");
        //PrintWriter out = res.getWriter();
                  ResultSet rs=null;
                String password1=null;
                String username1=null;
                String uname1=req.getParameter("username");
                
                String password=req.getParameter("password");
                Connection con=ConnectionFactory.getConnection();
                String sql=null;
                try {
                        Statement st=con.createStatement();
                        sql="select UNAME,PASSWORD from USERDB where uname='"+uname1+"' "+" AND password='"+password+"'";
                       
                        rs=st.executeQuery(sql);
                        System.out.println(rs);
                        RequestDispatcher rd=null;
                       
                        if(rs.next())
                        {
                        username1=rs.getString("uname");
                        password1=rs.getString("password");
                        
                        
                      
                        if(username1 != null && password1 != null && username1.equals(uname1) && password1.equals(password))
                        {
                                         
                                        rd=req.getRequestDispatcher("/citizens.jsp");
                                
                                rd.forward(req,res);
                                       
                               
                      }
                       else{
                                rd=req.getRequestDispatcher("/index.jsp");
                                rd.forward(req,res);
                                System.out.println("password");
                               
                               
                               
                        }
                      }
               
                    }             
                    catch (Exception e) {
                        // TODO Auto-generated catch block
                        
                        e.printStackTrace();
                    }
               
               ConnectionFactory.close(con);
               
                }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       // processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
   
    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    
    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    
}
